﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PatentFee.Entity;

namespace PatentFee.Bussines
{
    /// <summary>
    /// 年费信息操作类。
    /// </summary>
    public class AnnualFeeInfoBussines
    {
        /// <summary>
        /// 新增信息。
        /// </summary>
        /// <param name="annualFeeInfo">年费信息。</param>
        public void Add(AnnualFeeInfo annualFeeInfo)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                dbContext.AnnualFeeInfos.Add(annualFeeInfo);
                dbContext.SaveChanges();
            }
        }

        /// <summary>
        /// 修改年费信息。
        /// </summary>
        /// <param name="annualFeeInfo">年费信息。</param>
        public void Change(AnnualFeeInfo annualFeeInfo)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var entity = dbContext.AnnualFeeInfos.FirstOrDefault(p => p.Id == annualFeeInfo.Id);
                if (entity != null)
                {
                    entity = annualFeeInfo;
                    dbContext.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 删除年费信息。
        /// </summary>
        /// <param name="id">年费信息id。</param>
        public void Remove(int id)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var entity = dbContext.AnnualFeeInfos.FirstOrDefault(p => p.Id == id);
                if (entity != null)
                {
                    dbContext.AnnualFeeInfos.Remove(entity);
                    dbContext.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 根据id获取年费信息。
        /// </summary>
        /// <param name="id">年费id。</param>
        /// <returns>返回对应id的年费信息，如果没有则返回null。</returns>
        public AnnualFeeInfo GetAnnualFeeById(int id)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                return dbContext.AnnualFeeInfos.FirstOrDefault(p => p.Id == id);
            }
        }
    }
}
